﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Configuration;
using System.Web;
using HtmlAgilityPack;
using Newtonsoft.Json;
using WorldExchange;
using DatabaseService;
namespace WorldGoldenPrice
{
    public class GoldenPriceEasy : GoldenPrice
    {
        public Double OildPara;
        public override void getPrice()
        {
            this.VnBuy = "";
            this.VnSell = "";
            this.WorldBuy = "";
            this.WorldSell = "";
            this.LinkService = ConfigurationSettings.AppSettings["GoldPrice_WebService_Easy"].ToString().Trim();
            OildPara = Convert.ToDouble(ConfigurationSettings.AppSettings["OildPara"].ToString().Trim());
            XmlDocument xmldoc = new XmlDocument();
            try
            {
                string strXML = WebTool.getWebContent(this.LinkService);
                strXML = strXML.Substring(strXML.IndexOf("GetTableMids"));
                strXML = "{" + strXML.Substring(0, strXML.IndexOf("}") + 1) + "}";
                XmlNode xmlNode = JavaScriptConvert.DeserializeXmlNode(strXML);
                Single priceXAUUSD = Convert.ToSingle(xmlNode.SelectSingleNode("//XAUUSD").InnerText);
                Double priceOil = Convert.ToSingle(xmlNode.SelectSingleNode("//OILUSD").InnerText);
                this.EURUSD = xmlNode.SelectSingleNode("//EURUSD").InnerText;
                this.GBPUSD = xmlNode.SelectSingleNode("//GBPUSD").InnerText;
                this.USDJPY = xmlNode.SelectSingleNode("//USDJPY").InnerText;
                this.USDCAD = xmlNode.SelectSingleNode("//USDCAD").InnerText;
                this.AUDUSD = xmlNode.SelectSingleNode("//AUDUSD").InnerText;
                this.NZDUSD = xmlNode.SelectSingleNode("//NZDUSD").InnerText;
                //this.USDNOK = xmlNode.SelectSingleNode("//USDNOK").InnerText;

                //Tính giá thế giới mua/bán
                //Double sp = 0.00015;
                //Double pow = 10000;
                this.WorldBuy = (priceXAUUSD - 0.25).ToString("0.00");//(Math.Round(((priceXAGUSD - sp) * pow), 4) / pow).ToString();
                this.WorldSell = (priceXAUUSD + 0.25).ToString("0.00");//(Math.Round(((priceXAGUSD + sp) * pow), 4) / pow).ToString();
                this.OilBuy = (priceOil - OildPara).ToString("0.00");
                this.OilSell =(priceOil + OildPara).ToString("0.00");
             
                //Quy đổi ra Việt nam đồng
                Single wBuy = Convert.ToSingle(this.WorldBuy);
                Single wSell = Convert.ToSingle(this.WorldSell);

              
                Exchange_Factory TGNT = AccessDB.getExchangeInfo();
                Single exRate = Convert.ToSingle(TGNT.USD.Ban);

                this.VnBuy = (((wBuy + 0) * 1.0 * 1.205656 * exRate + 0) / 1000).ToString("#,###");
                this.VnSell = (((wSell + 0) * 1.0 * 1.205656 * exRate + 0) / 1000).ToString("#,###");

           }
            catch (Exception ex)
            {
                TLog.WriteErr("GoldenPriceEasy : getPrice", ex.ToString());
                
            }
            setNewValue();

            
        }

    }
}
